#!/bin/sh
################################################################################
# master
#
# This script configures MythTV master backend communication.
################################################################################
. /etc/rc.d/functions

start() {

    local WOLSqlConnectRetry
    local WOLSqlReconnectWaitTime
    local WOLSqlCommand
    local WOLSqlConnectAttempt
    local WOL_Additional_Delay
    local HOST_NAME

    /bin/touch /var/lib/init/master.inprogress

    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/master] Starting..."

    HOST_NAME=`/bin/hostname`

    # Determine broadcast address.
    MM_MASTER_WOL_BROADCAST=`/sbin/ifconfig ${MM_NETWORK_INTERFACE} | /bin/grep ' Bcast:' | /bin/sed 's%.* Bcast:\([^ ]*\) .*%\1%'`

    # Configure mysql.txt file.
    /bin/sed -i "s%@MM_HOSTNAME@%${HOST_NAME}%"                                               /home/minimyth/.mythtv/mysql.txt
    /bin/sed -i "s%@MM_MASTER_SERVER@%${MM_MASTER_SERVER}%"                                   /home/minimyth/.mythtv/mysql.txt
    /bin/sed -i "s%@MM_MASTER_DBUSERNAME@%${MM_MASTER_DBUSERNAME}%"                           /home/minimyth/.mythtv/mysql.txt
    /bin/sed -i "s%@MM_MASTER_DBPASSWORD@%${MM_MASTER_DBPASSWORD}%"                           /home/minimyth/.mythtv/mysql.txt
    /bin/sed -i "s%@MM_MASTER_DBNAME@%${MM_MASTER_DBNAME}%"                                   /home/minimyth/.mythtv/mysql.txt
    if [ "x${MM_MASTER_WOL_ENABLED}" = "xyes" ] ; then
        /bin/sed -i "s%@MM_MASTER_WOL_FALSE@%\#%"                                             /home/minimyth/.mythtv/mysql.txt
        /bin/sed -i "s%@MM_MASTER_WOL_TRUE@%%"                                                /home/minimyth/.mythtv/mysql.txt
    else
        /bin/sed -i "s%@MM_MASTER_WOL_FALSE@%%"                                               /home/minimyth/.mythtv/mysql.txt
        /bin/sed -i "s%@MM_MASTER_WOL_TRUE@%\#%"                                              /home/minimyth/.mythtv/mysql.txt
    fi
    /bin/sed -i "s%@MM_MASTER_WOLSQLRECONNECTWAITTIME@%${MM_MASTER_WOLSQLRECONNECTWAITTIME}%" /home/minimyth/.mythtv/mysql.txt
    /bin/sed -i "s%@MM_MASTER_WOLSQLCONNECTRETRY@%${MM_MASTER_WOLSQLCONNECTRETRY}%"           /home/minimyth/.mythtv/mysql.txt
    /bin/sed -i "s%@MM_MASTER_WOLSQLCOMMAND@%${MM_MASTER_WOLSQLCOMMAND}%"                     /home/minimyth/.mythtv/mysql.txt
    /bin/sed -i "s%@MM_MASTER_WOL_BROADCAST@%${MM_MASTER_WOL_BROADCAST}%"                     /home/minimyth/.mythtv/mysql.txt
    /bin/sed -i "s%@MM_MASTER_WOL_MAC@%${MM_MASTER_WOL_MAC}%"                                 /home/minimyth/.mythtv/mysql.txt

    # Configure config.xml file.
    /bin/sed -i "s%@MM_HOSTNAME@%${HOST_NAME}%"                                               /home/minimyth/.mythtv/config.xml
    /bin/sed -i "s%@MM_MASTER_SERVER@%${MM_MASTER_SERVER}%"                                   /home/minimyth/.mythtv/config.xml
    /bin/sed -i "s%@MM_MASTER_DBUSERNAME@%${MM_MASTER_DBUSERNAME}%"                           /home/minimyth/.mythtv/config.xml
    /bin/sed -i "s%@MM_MASTER_DBPASSWORD@%${MM_MASTER_DBPASSWORD}%"                           /home/minimyth/.mythtv/config.xml
    /bin/sed -i "s%@MM_MASTER_DBNAME@%${MM_MASTER_DBNAME}%"                                   /home/minimyth/.mythtv/config.xml

    # If using wake-on-lan, then make sure that the MythTV master backend is awake.
    if [ "x${MM_MASTER_WOL_ENABLED}" = "xyes" ] && [ ! mm_mythdb_test ] ; then
        WOLSqlConnectRetry=` \
            /bin/cat /home/minimyth/.mythtv/mysql.txt | \
            /bin/grep '^WOLSqlConnectRetry=' | \
            /bin/sed 's%WOLSqlConnectRetry=%%'`
        WOLSqlReconnectWaitTime=` \
            /bin/cat /home/minimyth/.mythtv/mysql.txt | \
            /bin/grep '^WOLSqlReconnectWaitTime=' | \
            /bin/sed 's%WOLSqlReconnectWaitTime=%%'`
        WOLSqlCommand=` \
            /bin/cat /home/minimyth/.mythtv/mysql.txt | \
            /bin/grep '^WOLSqlCommand=' | \
            /bin/sed 's%WOLSqlCommand=%%'`
        WOLSqlConnectAttempt=1
        while [ ${WOLSqlConnectAttempt} -le ${WOLSqlConnectRetry} ] && [ ! mm_mythdb_test ] ; do
            mm_message_output info "waking MythTV master backend database (${WOLSqlConnectAttempt} of ${WOLSqlConnectRetry} attempts) ..."
            ${WOLSqlCommand}
            /bin/sleep ${WOLSqlReconnectWaitTime}
            WOLSqlConnectAttempt=$((WOLSqlConnectAttempt+1))
        done
        # Wait the additional delay after MySQL is awake.
        if mm_mythdb_test ; then
            WOL_Additional_Delay=0
            while [ ${WOL_Additional_Delay} -le ${MM_MASTER_WOL_ADDITIONAL_DELAY} ] ; do
                mm_message_output info "waiting while MythTV master backend wakes (${WOL_Additional_Delay} of ${MM_MASTER_WOL_ADDITIONAL_DELAY} seconds) ..."
                /bin/sleep 1
                WOL_Additional_Delay=$((WOL_Additional_Delay+1))
            done
        fi
    fi

    # Test connectivity with master DB
    mm_test_master_db_connection show_on_splash

    /usr/bin/logger -s -t minimyth -p "local0.info" "[init.d/master] Started with return code=$?..."

    /bin/rm -f /var/lib/init/master.inprogress

    return $?
}

stop() {
    return 0
}

case $1 in
    start) start ;;
    stop)  stop  ;;
esac

exit 0
